package com.whz.enummap.plant;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: sinosoft
 * @Description: 使用枚举中的 ordinal() 充当索引
 * @Date: Create in 9:54 2021/5/17
 * @Modified By:
 */
public class PlantMainTest_Ordinal {
    public static void main(String[] args) {

        Plant[] garden = new Plant[] {
                new Plant("白菊花", Plant.LifeCycle.ANNUAL),
                new Plant("黄菊花", Plant.LifeCycle.ANNUAL),
                new Plant("橄榄菊", Plant.LifeCycle.BIENNIAL),
                new Plant("银冠花", Plant.LifeCycle.PERENNIAL)
        };

        Set<Plant>[] plantByLifeCycle =
                (Set<Plant>[]) new Set[Plant.LifeCycle.values().length];

        for (int i = 0; i < plantByLifeCycle.length; i++) {
            plantByLifeCycle[i] = new HashSet<>();
        }
        for (Plant p : garden) {
            plantByLifeCycle[p.lifeCycle.ordinal()].add(p);
        }

        /*输出结果*/
        for (int i = 0; i < plantByLifeCycle.length; i++) {
            System.out.printf("%s: %s%n", Plant.LifeCycle.values()[i], plantByLifeCycle[i]);
        }
    }
}
